package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.service.cache.CommodityServiceCache;
import com.wan37.protobuf.constant.CommonShop;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameShop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-06 15:30
 **/
@Component(PtoNum.SSHOPRESPONSE + "")
public class SShopResponse implements ReceiveMsg {
    private final static Logger logger = LoggerFactory.getLogger(SShopResponse.class);
    @Autowired
    private CommodityServiceCache commodityServiceCache;

    @Override
    public void execute(Message msg) {
        GameShop.SShopResponse sShopResponse = (GameShop.SShopResponse) msg;
        int code = sShopResponse.getCode();
        switch (code) {
            case CommonShop.GAME_GET_ALL_COMMODITY:
                sShopResponse.getCommodityList().forEach(commodity -> {
                    CommodityServiceCache.CommodityIn commodityIn = commodityServiceCache.new CommodityIn();
                    commodityIn.setCommodity(commodity);
                    commodityServiceCache.getCommodityInMap().put(commodity.getId(), commodityIn);
                });
                logger.info("商城信息初始化成功！");
                break;
            case CommonShop.GAME_SHOP_COMMODITY:
                logger.info("商品购买成功！");
                break;
            case CommonShop.GAME_ERROR:
                logger.info("购买失败！{}",sShopResponse.getContent());
                break;
            default:
                break;
        }
    }
}
